﻿// // ******************************************************************
// //       /\ /|       @file       ResultExtension.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2024-11-08 20:11
// //    *(__\_\        @Copyright  Copyright (c) , 2024-11-03 08:11 Await
// // ******************************************************************

namespace AutomaticSubtitles.Service.Infrastructure.Extensions;

public static class ResultExtension
{
    public const string deleteMsg = "删除成功";
    public const string updateMsg = "修改成功";
    public const string queryMsg = "查询成功";
    public const string addMsg = "添加成功";

    public static ResponseModel<string> CreateExceptionResult(this Exception exception, int code)
    {
        return new ResponseModel<string>(code, exception.InnerException?.Message ?? exception.Message);
    }

    public static ResponseModel<T?> CreateResult_Msg<T>(this T data, string msg = queryMsg, bool isShow = true)
    {
        return ReturnResponseModel(data, msg, isShow);
    }

    public static ResponseModel<T?> CreateResult<T>(this T? data, string msg = queryMsg, bool isShow = false)
    {
        return ReturnResponseModel(data, msg, isShow);
    }

    private static ResponseModel<T?> ReturnResponseModel<T>(T? data, string msg, bool isShow)
    {
        if (data is int && data.Equals(0) && msg is updateMsg or deleteMsg or addMsg) return new ResponseModel<T>(Erro, data, "操作失败");
        var responseModel = new ResponseModel<T?>(Success, data, msg);
        if (isShow) responseModel.code++;
        return responseModel;
    }

    public static ResponseModel<T?> CreateResultAdd_Msg<T>(this T? data, bool isShow = true)
    {
        return CreateResult(data, addMsg, isShow);
    }

    public static ResponseModel<T?> CreateResultAdd<T>(this T? data, bool isShow = false)
    {
        return CreateResult(data, addMsg, isShow);
    }

    public static ResponseModel<T?> CreateResultDelete_Msg<T>(this T? data, bool isShow = true)
    {
        return CreateResult(data, deleteMsg, isShow);
    }

    public static ResponseModel<T?> CreateResultDelete<T>(this T? data, bool isShow = false)
    {
        return CreateResult(data, deleteMsg, isShow);
    }

    public static ResponseModel<T?> CreateResultUpdate<T>(this T? data, bool isShow = false)
    {
        return CreateResult(data, updateMsg, isShow);
    }

    public static ResponseModel<T?> CreateResultUpdate_Msg<T>(this T? data, bool isShow = true)
    {
        return CreateResult(data, updateMsg, isShow);
    }

    public static ResponseModel<T> Fail<T>(this T data, int code = Erro)
    {
        return new ResponseModel<T>(code, data, "操作失败");
    }
}